<template>
  <div>
    <el-dialog
      :title="obj.isAdd == true ? '商品添加' : '商品编辑'"
      :visible.sync="obj.isTrue"
      @opened="opened()"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="一级分类">
          <el-select
            v-model="form.first_cateid"
            placeholder="请选择一级分类"
            @change="changeone"
          >
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="(item, index) in cateList"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="form.second_cateid" placeholder="请选择二级分类">
            <el-option
              :label="item.catename"
              :value="item.id"
              v-for="item in secondarr"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品名称">
          <el-input v-model="form.goodsname"></el-input>
        </el-form-item>

        <el-form-item label="价格">
          <el-input v-model="form.price"></el-input>
        </el-form-item>

        <el-form-item label="市场价格">
          <el-input v-model="form.market_price"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="changeimg"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="商品规格">
          <el-select
            v-model="form.specsid"
            placeholder="请选择活动区域"
            @change="changetow"
          >
            <el-option
              :label="item.specsname"
              :value="item.id"
              v-for="item in specsList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="规格属性">
          <el-select
            v-model="form.specsattr"
            multiple
            placeholder="请选择活动区域"
          >
            <el-option
              :label="item"
              :value="index"
              v-for="(item, index) in specsattr"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否新品">
          <el-radio :label="1" v-model="form.isnew">是</el-radio>
          <el-radio :label="2" v-model="form.isnew">否</el-radio>
        </el-form-item>
        <el-form-item label="是否热卖">
          <el-radio :label="1" v-model="form.ishot">是</el-radio>
          <el-radio :label="2" v-model="form.ishot">否</el-radio>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
        <el-form-item>
          <div id="div1" v-if="obj.isTrue"></div>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submit()" v-if="obj.isAdd == false"
          >添加</el-button
        >
        <el-button type="primary" v-else @click="dit()">编辑</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  cateList,
  goodsAdd,
  goodsInfo,
  goodsEdit,
} from "../../../requster/api";
import { successAlert, warningAlert } from "../../../util/alter";
import E from "wangeditor";
export default {
  props: ["obj"],
  components: {},
  data() {
    return {
      imageUrl: "",
      // 渲染二级分类
      secondarr: [],
      // 渲染规格属性
      specsattr: [],
      form: {
        first_cateid: "",
        second_cateid: "",
        goodsname: "",
        price: "",
        market_price: "",
        img: "",
        description: "",
        specsid: "",
        specsattr: [],
        isnew: 1,
        ishot: 1,
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      cateList: "cate/list",
      specsList: "specs/list",
    }),
  },
  methods: {
    //   动画结束之后
    opened() {
      this.editor = new E("#div1");
      this.editor.create();

      // 给富文本编辑器设置内容
      this.editor.txt.html(this.form.description);
    },
    //   获取一条信息
    getone(id) {
      goodsInfo({ id: id }).then((res) => {
        this.form = res.data.list;
        // 根据拿到的一级分类渲染二级分类
        cateList({ pid: this.form.first_cateid }).then((res) => {
          this.secondarr = res.data.list;
        });
        this.imageUrl = this.$pre + this.form.img;
        let data = this.specsList.find((item) => item.id == this.form.specsid);
        this.specsattr = data.attrs;
        this.form.specsattr = this.form.specsattr.split(",");
        this.form.id = id;
      });
    },
    //   点击编辑
    dit(id) {
      this.form.specsattr = JSON.stringify(this.form.specsattr);
      goodsEdit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.reqgoodslist();
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    //   清空封装
    empt() {
      this.imageUrl = "";
      (this.secondarr = []),
        // 渲染规格属性
        (this.specsattr = []),
        (this.form = {
          first_cateid: "",
          second_cateid: "",
          goodsname: "",
          price: "",
          market_price: "",
          img: "",
          description: "",
          specsid: "",
          specsattr: [],
          isnew: 1,
          ishot: 1,
          status: 1,
        });
    },
    // 点击取消
    cancel() {
      this.obj.isTrue = false;
      this.empt();
    },
    // 点击添加
    submit() {
      this.form.description = this.editor.txt.html();
      this.form.specsattr = JSON.stringify(this.form.specsattr);
      goodsAdd(this.form).then((res) => {
        if (res.data.code == 200) {
          this.cancel();
          successAlert(res.data.msg);
        } else {
          warningAlert(res.data.msg);
        }
      });
    },
    ...mapActions({
      reqgoodslist: "goods/reqchangelist",
      reqcatelist: "cate/reqChangeList",
      reqspecslist: "specs/reqchangelist",
    }),
    // 当一级分类改变
    changeone() {
      cateList({ pid: this.form.first_cateid }).then((res) => {
        this.secondarr = res.data.list;
        // 清空二级分类
        this.form.second_cateid = "";
      });
    },
    // 商品规格
    changetow() {
      // console.log(123);
      // 查询数据中与当前选中的id相同的数据将赋值
      let data = this.specsList.find((item) => item.id == this.form.specsid);
      this.specsattr = data.attrs;
      this.form.specsattr = [];
      console.log(this.specsList);

      console.log(data);
    },
    // 图片处理
    changeimg(e) {
      this.imageUrl = URL.createObjectURL(e.raw);
      this.form.img = e.raw;
    },
  },
  mounted() {
    this.reqcatelist();
    this.reqspecslist(true);
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>